<?php
namespace app\index\controller;

use app\common\logic\WalletLogic;
use think\facade\Config;
use app\common\logic\UserLogic;
use app\common\logic\OrderLogic;
use app\common\logic\ProductLogic;
use app\index\controller\Wecpay;
use think\Container;
use think\response\Json;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;
use app\index\server\BonusPaymentService;
/**
 * 订单支付成功后处理类
 */
class Payment extends Common {
    protected $wechant_numb;

    public function initialize() {
        parent::initialize();
        $this->alipay = config('alipay');
    }

    public function index()
    {
        $order = input('post.order');
        $pay = new BonusPaymentService();
        $rel = $pay->index($order);
        if ($rel['code'] == 1) {
            return $rel['msg'];
        } else {
            return $rel['msg'];
        }
    }


    // 接收支付宝支付信息
    public function aliNotify()
    {
        //  写入文件
        $this->writelog("weixinnotify_url.txt", '支付宝-走进来了' . date("Y-m-d H:i:s") . "\r\n");
        $config = config('alipay');
        $result = Pay::alipay($config)->verify();
        if ($result) {
            // 写入文件
            $this->writelog("weixinnotify_url.txt", '支付宝-验签成功' . date("Y-m-d H:i:s") . "\r\n");
            $arr = input('post.');
            $order_type = model('order')->where('or_num', $arr['out_trade_no'])
                ->value('or_status');
            if ($order_type == 0) {
                if ($arr['trade_status'] == 'TRADE_SUCCESS') {
                    db('order')->where('or_num',$arr['out_trade_no'])->update(['pay_type'=>3]);
                    $pay = new BonusPaymentService();
                    $rel = $pay->index($arr['out_trade_no']);
                    if ($rel) {
                        return Pay::alipay($config)->success()->send();
                    }
                }
            } else {
                return Pay::alipay($config)->success()->send();
            }
        } else {
            //写入文件
            $this->writelog("weixinnotify_url.txt", '支付宝-验签失败' . date("Y-m-d H:i:s") . "\r\n");
            echo "fail";
            exit;
        }
    }


    // 微信支付异步接收地址
    public function cz_notify(){
        $this->writelog("weixinnotify_url.txt",'微信-进来'.date("Y-m-d H:i:s")."\r\n");
        $config=config('wechat');
        $result = Pay::wechat($config)->verify();
        $this->writelog("weixinnotify_url.txt",'微信-验证'.date("Y-m-d H:i:s")."\r\n");
        if($result){
            $xml = post_data();
            $arr=xmlToArray($xml);
            if($arr['return_code']=='SUCCESS'){
                //$this->writelog("weixinnotify_url.txt",'成功'.$arr['out_trade_no'].date("Y-m-d H:i:s")."\r\n");
                $rel = $this->allpay($arr['out_trade_no']);
                if ($rel['code'] == 0) {
                    return json($rel);
                }elseif ($rel['code'] == 1) {
                    $this->s_msg('微信支付成功');
                }
                // $chongzhi=db('tixian')->where('num',$arr['out_trade_no'])->find();
                // if(!$chongzhi){
                //     echo "fail";exit;
                // }
                // if($chongzhi['tx_review']==4){
                //     $res=$this->recharge($chongzhi['us_id'],$chongzhi['tx_sum'],$chongzhi['id']);
                //     if($res){
                //         return Pay::wechat($config)->success()->send();
                //     }
                // }
            }else{
                $this->writelog("weixinnotify_url.txt",'微信-失败'.date("Y-m-d H:i:s")."\r\n");
            }
        }
    }


    //写入日志
    public function writelog($file,$content)
    {
        // $file = "log.txt";
        //  $content = "内容标题\r\n内容第一行\r\n内容第二行";

        // 打开文件$file时，使用追加模式，此时文件指针会在文件开始出
        if(!$fp = fopen($file,'a'))
        {
            echo "打开文件.$file.失败！";
            exit;
        }

        if(fwrite($fp,$content) === FALSE)
        {
            echo "写入文件失败！";
            exit;
        }

        echo "写入文件成功！";
        fclose($fp);


    }



// 	public function message()
// 	{
// 		$myfile = "log.txt";
// 		$txt = date('Y-m-d H:i:s')."^_^".'写入文件'."\r\n";
// 		$this->writelog($myfile,$txt);
// 	}
// 	function xmlToArray($xml)
// 	{
// 	    //禁止引用外部xml实体
// 	    libxml_disable_entity_loader(true);
// 	    $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
// 	    return $values;
// 	}

// //微信异步通知获取post数据函数
// function post_data()
// {
//     //$receipt = $_REQUEST;
//     $receipt=null;
//     if($receipt==null){
//         $receipt = file_get_contents("php://input");
//         if($receipt == null){
//             $receipt = $GLOBALS['HTTP_RAW_POST_DATA'];
//         }
//     }
//     return $receipt;
// }
// 	public function pay()
// 	{
// 		$config = config('alipay');
// 		$order = [
// 		    'out_trade_no' => time(),
// 		    'total_amount' => '0.01',
// 		    'subject' => '特价药品',
// 		];

// 		$alipay = Pay::alipay($config)->web($order);
// 		$this->assign('alipay',$alipay);
// 		return $this->fetch();
// 	}
// 	public function wepay()
// 	{
// 		$wechat = config('wechat');
// 		$order = [
// 		    'out_trade_no' => time(),
// 		    'body' => '特价药品',
// 		    'total_fee' => '1',
// 		];
// 		$wepay=Pay::wechat($wechat)->wap($order);
// 		$this->assign('wepay',$wepay);
// 		return $this->fetch();
// 	}


}